﻿using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class HouseGenerator : MonoBehaviour {

	public TurnSystem _turnSystem;

	public float _widith;

	public float _height;

	public Vector3 _buffer;

	public TextAsset _houseCordinates;

	public GameObject soundman;

	public TextAsset _houseConnections;

	private Transform _housePrefab;

	public House _templateHouse;

	public Canvas _canvase;

	public RectTransform _spawnArea;


	// Use this for initialization
	void Start () {
	

		string [] rows = _houseCordinates.text.Split('\n');

		Vector2 invserSpawnRatio = _spawnArea.anchorMin;
		Vector2 spawnRatio = new Vector2(1 - invserSpawnRatio.x, 1 - invserSpawnRatio.x);
		Vector2 spawnArea = new Vector2 (Screen.width * spawnRatio.x, Screen.height * spawnRatio.y);


		Vector2 coordArea = new Vector2 (_widith, _height);

		Vector2 scaleFactor = new Vector2 ( spawnArea.x /  coordArea.x, spawnArea.y / coordArea.y);


		foreach (string row in rows) {
			string [] xy = row.Split(',');


			Debug.Log ("'" + row + "'");

			float x = Convert.ToInt32(xy[1]) *  scaleFactor.x + Screen.width * invserSpawnRatio.x;
			float y = Convert.ToInt32(xy[2]) * scaleFactor.y + Screen.height * invserSpawnRatio.y;

			Vector3 housePosition = new Vector3(x, y, 0)  + _buffer;

			//Vector3 housePosition = _spawnArea.anchorMax;
			House house =  Instantiate(_templateHouse,  housePosition, Quaternion.Euler(0,0,0)) as House;

			house.soundman = soundman;

			house.occupantsno = (int)UnityEngine.Random.Range (1,8);

			house.UpdateOccs();

			house.transform.SetParent(_canvase.transform);
			house.SetTurnSystem(_turnSystem);
			_turnSystem._houses.Add(house);

			// setup all the event junk

			house._turnSystem = _turnSystem;


			//EventTrigger eventTrigger = house.GetComponent<EventTrigger>();

			//eventTrigger.AddListener((eventData) => new UnityAction<House>(house));

			//EventTrigger.Entry entry = new EventTrigger.Entry(){ callback = new EventTrigger.TriggerEvent(), eventID =  EventTriggerType.PointerClick};
			//UnityAction<House> call = new UnityAction<House>(_turnSystem.selectHouse());
			//entry.callback.AddListener(call);


			//entry.eventID = EventTriggerType.PointerClick;
			//entry.callback = new EventTrigger.TriggerEvent();

			//eventTrigger.delegates.Add(entry);

		}

		rows = _houseConnections.text.Split('\n');
				
		for (int index = 0; index < rows.Length; index++) {
			string [] connections = rows[index].Split(',');

			for (int count = 0; count < connections.Length; count++) {
				if (connections[count].Equals("1")) {
					_turnSystem._houses[index].AddNeighbour(_turnSystem._houses[count]);
				}
			}

			//foreach (string connection in connections) {
			//	mMapOfHouses[index].AddNeighbour(mMapOfHouses[Convert.ToInt32(connection)]);
			//}
		}

		//mMapOfHouses.Add ();

		//for (_houseCordinates.text) {

		//}
		_turnSystem._houses [0].startFire ();
		_turnSystem._houses [1].startFire ();
		_turnSystem._houses [2].startFire ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

}
